% Cohort and Duration Transprob cacluation with conditions:
% Rates {1+1.5, 2,2.5,3,3.5,4, 4.5+5+5.5, -1};
% maxPPDOS_sum constrains is controlled by variable 'conditions' in loop
% cdIdex

clear all
clc

conn = database('BDC','','');
labels = {1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,-1};
edges = [2 3 4 5 6 7 10 11 12];
edgesDur = [2 3 4 5 6 7 10 11];

year_list=1997:2009;
DURATION = 1;
NUMBER_OF_YEAR = length(year_list) - DURATION;
conditions = {'maxPPDOS_sum >= 50000','maxPPDOS_sum >= 50000 and maxPPDOS_sum < 250000',...
    'maxPPDOS_sum >= 250000 and maxPPDOS_sum <1000000','maxPPDOS_sum >= 1000000'};
numRate = length(labels)+1; % add column for WR group
numEdges = length(edges);
numEdgesStd = numEdges-1;
numGrp = length(conditions);

cohTp = zeros(numGrp, NUMBER_OF_YEAR, numRate, numRate);
cohTpGrp = zeros(numGrp, NUMBER_OF_YEAR, numEdges, numEdges);
cohTpGrpStd =zeros(numGrp, NUMBER_OF_YEAR, numEdgesStd, numEdgesStd);
cohTtlStdVec = zeros(numGrp, NUMBER_OF_YEAR, 1, numEdgesStd);
cohTtlStdMat = zeros(numGrp, NUMBER_OF_YEAR, numEdgesStd, numEdgesStd);
durTp = zeros(numGrp, NUMBER_OF_YEAR, numRate, numRate);
durTpGrp = zeros(numGrp, NUMBER_OF_YEAR, numEdges, numEdges);
durTpGrpStd =  zeros(numGrp, NUMBER_OF_YEAR, numEdgesStd, numEdgesStd);
durTtlStdVec = zeros(numGrp, NUMBER_OF_YEAR, 1, numEdgesStd);
durTtlStdMat = zeros(numGrp, NUMBER_OF_YEAR, numEdgesStd, numEdgesStd);
avgCohTpGrpStd = zeros(numGrp,numEdgesStd, numEdgesStd);
avgDurTpGrpStd = zeros(numGrp,numEdgesStd, numEdgesStd);


for cdIdx = 1:numGrp
    disp(conditions(cdIdx));
    for yearIdx=1:NUMBER_OF_YEAR
        
        Date1 = strcat(num2str(year_list(yearIdx)),'-12-01');
        Date2 = strcat(num2str(year_list(yearIdx+DURATION)),'-12-01');
        disp( strcat(['Date: ' Date1 ' to ' Date2]));
        
        condition = char(conditions(cdIdx));
        sqlquery1 = strcat(['select Customer, modiFisDate, modiRate FROM final_v4_tbl where ' condition ' and modiFisDate BETWEEN ''' Date1 ''' and ''' Date2 ''' order by customer, modiFisDate']);
        results = fetch(conn, sqlquery1);
        disp(size(results));
        
        % cohort
        [cohTp(cdIdx,yearIdx,:,:),ttl] = transprobwr(results, 'labels',labels,'algorithm','cohort');
        TpGrp = transprobgrouptotals(ttl,edges);
        cohTpGrp(cdIdx,yearIdx,:,:) = transprobbytotalswr(TpGrp);
               
        TpGrpStd.totalsVec = sum(TpGrp.totalsMat(1:numEdgesStd,1:numEdgesStd),2)';
        TpGrpStd.totalsMat = TpGrp.totalsMat(1:numEdgesStd,1:numEdgesStd);
        TpGrpStd.algorithm = TpGrp.algorithm;
        cohTpGrpStd(cdIdx,yearIdx,:,:) = transprobbytotals(TpGrpStd);
        cohTtlStdVec (cdIdx,yearIdx,1,:) = TpGrpStd.totalsVec;
        cohTtlStdMat (cdIdx,yearIdx,:,:) = TpGrpStd.totalsMat;        
        disp('Cohort tranprob is done')
        
        % duration
        [durTp(cdIdx,yearIdx,:,:),ttl] = transprobwr(results, 'labels',labels); 
        TpGrp = transprobgrouptotals(ttl,edges);
        durTpGrp(cdIdx,yearIdx,:,:) = transprobbytotals(TpGrp);
                
        TpGrpStd.totalsVec = TpGrp.totalsVec(1:numEdgesStd);
        TpGrpStd.totalsMat = TpGrp.totalsMat(1:numEdgesStd,1:numEdgesStd);
        TpGrpStd.algorithm = TpGrp.algorithm;
        durTpGrpStd(cdIdx,yearIdx,:,:) = transprobbytotals(TpGrpStd);
        durTtlStdVec (cdIdx,yearIdx,1,:) = TpGrpStd.totalsVec;
        durTtlStdMat (cdIdx,yearIdx,:,:) = TpGrpStd.totalsMat;      
        disp('Duration tranprob is done')
        
        
    end
    
    %To calculate average tranprob
    
    %cohort
    avgCohTotalVec(:,:) = sum(cohTtlStdVec(cdIdx,:,:,:),2);
    avgCohTotalMat(:,:) = sum(cohTtlStdMat(cdIdx,:,:,:),2);
    sampleTotals = struct('totalsVec',avgCohTotalVec,'totalsMat',avgCohTotalMat,'algorithm','cohort');
    avgCohTpGrpStd(cdIdx,:,:) = transprobbytotals(sampleTotals);
    
    % duration
    avgDurTotalVec(:,:) = sum(durTtlStdVec(cdIdx,:,:,:),2);
    avgDurTotalMat(:,:) = sum(durTtlStdMat(cdIdx,:,:,:),2);
    sampleTotals = struct('totalsVec',avgCohTotalVec,'totalsMat',avgCohTotalMat,'algorithm','duration');
    avgDurTpGrpStd(cdIdx,:,:) = transprobbytotals(sampleTotals);
    
end
disp('finish')
save ('D:\My Documents\CIRANO\db_mySQL\tp_2011May29_MR7_WR_case2.mat')

%% block to calculate Q for specific transitions
% diagInd = logical(eye(numEdges,numEdges));
% totalsVec(:,:) = durTtlVec (grp,yr,1,:);
% totalsMat(:,:) = durTtlMat (grp,yr,:,:);
% totalsMat(diagInd) = 0;
% totalsMat(diagInd) = -sum(totalsMat,2);
% nonEmpty = find(totalsVec>0);
% transMat(nonEmpty,:) = bsxfun(@ldivide,...
% totalsVec(nonEmpty),totalsMat(nonEmpty,:));